 // 页面加载完成后开始渲染表格数据
$(window).on('load', function() {
	$.ajax({
		url: 'http://192.168.0.144:8080/digitalhome-boss/rest/Admin/pusers/getPusersList',
		type: 'GET',
		dataType: 'JSON',
		success: function(res) {

			if( '0000' == res.result_code ) 
			{ 
				// 渲染表格数据
				$(res.result).each(function() {
					personalJSON.createPersonalList(this.userName, this.displayName, this.email, this.createDate, this.status, this.userId);
				});

				// 个人用户详细信息
				$('.icon-details').off().on('click', function() {
					
					personalJSON.detailsModalFn();

					personalJSON.userId = $(this).attr('data-userId');

					console.log(personalJSON.userId);
					$.ajax({
						url: 'http://192.168.0.144:8080/digitalhome-boss/rest/Admin/pusers/getPusersInfo/{'+personalJSON.userId+'}',
						type: 'POST',
						dataType: 'JSON',
						success: function(res) {

							console.log(res.message);
							if( '0000' == res.result_code ) 
							{
								$(res.result).each(function() {
									personalJSON.detailsModalFn(this.userId, this.displayName, this.password, this.email, this.registerSource, this.portrait, this.createDate, this.updatePasswordDate, this.lastLoginDate, this.status, this.isActivate, this.isAutonym, this.userFolder);
								});
							}
							else
							{

							}
							
						},
						error: function() {

						}
					});
				    
				        
				});

				// 修改个人用户详细信息
				$('.icon-edit').off().on('click', function() {
					personalJSON.editFn();
				});

				// 删除个人用户详细信息
				$('.icon-delete').off().on('click', function() {
					personalJSON.deleteFn( $(this) );
				});
				
			}
			else
			{
				$('#personal-user-tbody-no-data').attr('class','text-center');
			}

		},
		error: function(err) {
			$('#personal-user-tbody-no-data').attr('class','text-center');
		}
	});
});


var personalJSON = {

	// 只能创建一次dom节点
    onceModal: [ 'newlyAdded', 'details', 'edit', 'delete' ],

    // userId
    userId: null,

    // 个人用户详细信息     功能
    detailsFn: function() {
		if( personalJSON.onceModal[1] ) 
	    {
	        personalJSON.detailsModalFn();
	        personalJSON.onceModal[1] = false;
	    }
    },

    // 修改个人用户详细信息 功能
    editFn: function() {
    	if( personalJSON.onceModal[2] ) 
	    {
	        personalJSON.editModalFn();
	        personalJSON.onceModal[2] = false;
	    }
    },

    // 删除个人用户详细信息 功能
    deleteFn: function(_this) {
    	if( personalJSON.onceModal[3] ) 
	    {
	        personalJSON.deleteModalFn();
	        personalJSON.onceModal[3] = false;
	    }

	    $('.confirm-del-btn').off().on('click', function() {
	        _this.parents('tr').remove();
	    });
    },

    // 新增
    newlyAdded: function() {
    	
    },

    // 个人用户详细信息     HTML标签
    detailsModalFn: function(_userId) {
    	var detailsModal = 
    		'<div class="modal fade" id="details" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'+
                '<div class="modal-dialog">'+
                    '<div class="modal-content">'+
                        '<div class="modal-header">'+
                            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'+
                            '<h4 class="modal-title" id="myModalLabel">个人用户详细信息</h4>'+
                        '</div>'+
                        '<div class="modal-body">'+
                            
                            '<form>'+
                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="dhName">用户名：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="dhName"   class="form-control" value='+_userId+'   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="dhName">昵称：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="dhName"   class="form-control" id="dhName"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="dhName">密码：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="dhName"   class="form-control" id="dhName"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="keywords">邮箱：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="keywords" class="form-control" id="keywords" disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="minAge">注册来源：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="minAge"   class="form-control" id="minAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">头像：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">注册时间：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">密码修改时间：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">最后登录时间：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">用户状态：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">邮箱激活状态：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">实名认证状态：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">用户文件夹：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">所属角色：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge"   disabled/>'+
                                    '</div>'+
                                '</div>'+

                            '</form>'+

                        '</div>'+
                        '<div class="modal-footer">'+
                            '<div class="col-sm-12">'+
                                '<button type="button" class="btn btn-primary" data-dismiss="modal">返回列表</button>'+
                            '</div>  '+
                        '</div>'+
                    '</div>'+
                '</div>'+
            '</div>';
            $(document.body).append(detailsModal);

    },

    // 修改个人用户详细信息 HTML标签
    editModalFn: function() {
    	var editModal = 
    		'<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'+
                '<div class="modal-dialog">'+
                    '<div class="modal-content">'+
                        '<div class="modal-header">'+
                            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'+
                            '<h4 class="modal-title" id="myModalLabel">个人用户详细信息</h4>'+
                        '</div>'+
                        '<div class="modal-body">'+
                            
                            '<form>'+
                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="dhName">用户名：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="dhName"   class="form-control" id="dhName" autocomplete="off"  disabled/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="dhName">昵称：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="dhName"   class="form-control" id="dhName" autocomplete="off"  />'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="dhName">密码：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="dhName"   class="form-control" id="dhName" autocomplete="off"  />'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="keywords">邮箱：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="keywords" class="form-control" id="keywords" autocomplete="off"/>'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="minAge">注册来源：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="minAge"   class="form-control" id="minAge" autocomplete="off"  />'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">头像：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge" autocomplete="off"  />'+
                                    '</div>'+
                                '</div>'+

                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="maxAge">用户文件夹：</label>'+
                                    '<div class="col-sm-6">'+
                                       '<input type="text" name="maxAge"   class="form-control" id="maxAge" autocomplete="off"  />'+
                                    '</div>'+
                                '</div>'+

                                
                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="disabled">用户状态：</label>'+
                                    '<div class="col-sm-6">'+
                                        '<select id="disabled" class="form-control">'+
                                           '<option>禁用</option>'+
                                           '<option>启用</option>'+
                                        '</select>'+
                                    '</div>'+
                                '</div>'+

                                
                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="disabled">邮箱激活状态：</label>'+
                                    '<div class="col-sm-6">'+
                                        '<select id="disabled" class="form-control">'+
                                           '<option>已激活</option>'+
                                           '<option>未激活</option>'+
                                        '</select>'+
                                    '</div>'+
                                '</div>'+

                                
                                '<div class="form-group">'+
                                    '<label class="col-sm-3 control-label text-right" for="disabled">实名认证状态：</label>'+
                                    '<div class="col-sm-6">'+
                                        '<select id="disabled" class="form-control">'+
                                           '<option>已实名</option>'+
                                           '<option>未实名</option>'+
                                        '</select>'+
                                    '</div>'+
                                '</div>'+

                            '</form>'+

                        '</div>'+
                        '<div class="modal-footer">'+
                            '<div class="col-sm-12">'+
                                '<button type="button" class="btn btn-primary" data-dismiss="modal">保存</button>'+
                            '</div>  '+
                        '</div>'+
                    '</div>'+
                '</div>'+
            '</div>';
            $(document.body).append(editModal);
    },

    // 删除个人用户详细信息 HTML标签
    deleteModalFn: function() {
        var deleteModal = 
            '<div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'+
                '<div class="modal-dialog delete-modal">'+
                    '<div class="modal-content">'+
                        '<div class="modal-header">'+
                            '<h4 class="modal-title text-center">删除警告</h4>'+
                        '</div>'+
                        '<div class="modal-body text-center">请确认是否删除该行个人用户详细信息</div>'+
                        '<div class="modal-footer">'+
                            '<div class="col-md-6">'+
                                '<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'+
                            '</div>'+
                            '<div class="col-md-6">'+
                                '<button type="button" class="btn btn-primary pull-left confirm-del-btn" data-dismiss="modal">确定</button>'+
                            '</div>'+
                        '</div>'+
                    '</div>'+
                '</div>'+
            '</div>';
            $(document.body).append(deleteModal);
    },

    // 创建个人用户信息列表 HTML标签
    createPersonalList: function(_userName, _displayName, _email, _createDate, _status, _userId) {
    	var personalList = 
		    '<tr>'+
		        '<td class="text-center">'+_userName+'</td>'+
		        '<td class="text-center">'+_displayName+'</td>'+
		        '<td class="text-center">'+_email+'</td>'+
		        '<td class="text-center">'+_createDate+'</td>'+
		        '<td class="text-center">'+_status+'</td>'+
		        '<td class="text-center icon-td">'+
		            '<i class="icon-deny"    title=""         data-userId='+_userId+' data-toggle="modal" data-target="#deny"></i>'+
		            '<i class="icon-packing" title=""         data-userId='+_userId+' data-toggle="modal" data-target="#packing"></i>'+
		            '<i class="icon-SRS"     title=""         data-userId='+_userId+' data-toggle="modal" data-target="#SRS"></i>'+
		            '<i class="icon-details" title="详细信息" data-userId='+_userId+' data-toggle="modal" data-target="#details"></i>'+
		            '<i class="icon-edit"    title="编辑"     data-userId='+_userId+' data-toggle="modal" data-target="#edit"></i>'+
		            '<i class="icon-delete"  title="删除"     data-userId='+_userId+' data-toggle="modal" data-target="#delete"></i>'+
		        '</td>'+
		    '</tr>';
	    $('#personal-user-tbody').prepend(personalList);
    }

}